package com.meiyu.live;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Toast;

import com.android.volley.Response;
import com.netease.nimlib.sdk.NIMClient;
import com.netease.nimlib.sdk.RequestCallback;
import com.netease.nimlib.sdk.auth.AuthService;
import com.netease.nimlib.sdk.auth.LoginInfo;
import com.umeng.socialize.UMAuthListener;
import com.umeng.socialize.UMShareAPI;
import com.umeng.socialize.UMShareListener;
import com.umeng.socialize.bean.SHARE_MEDIA;
import com.meiyu.live.comm.Constant;
import com.meiyu.live.comm.UserConfig;
import com.meiyu.live.net.param.BaseBodyParams;
import com.meiyu.live.net.param.BetParams;
import com.meiyu.live.net.param.GetInfosParams;
import com.meiyu.live.net.param.LoginParams;
import com.meiyu.live.net.response.BaseResponse;
import com.meiyu.live.net.response.DealResponse;
import com.meiyu.live.net.response.GetInfosResponse;
import com.meiyu.live.net.response.LoginResponse;

import java.util.Map;

import cn.jpush.android.api.JPushInterface;

/**
 * Created by zbl on 2016/8/15.
 */
public class TestActivity extends BaseActivity implements Response.Listener<BaseResponse>, View.OnClickListener {

    UMShareAPI mShareAPI;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_test);
        mShareAPI = UMShareAPI.get(this);
        init();
        request(new LoginParams(13933333333L, "000000", JPushInterface.getRegistrationID(getApplicationContext())), LoginResponse.class, this);
        //第三方登录接口
//        request(new ThirdLoginParams("32131231","qq","f","sdfsdf","sdfsdf", JPushInterface.getUdid(this)), LoginResponse.class, this);
        Log.e("deviceId", JPushInterface.getUdid(this));

        // 添加微信平台，APP_ID、APP_SECRET都是在微信开放平台，移动应用通过审核后获取到的
//        mShareAPI.deleteOauth(this,SHARE_MEDIA.WEIXIN,umAuthListener);
//        mShareAPI.doOauthVerify(this, SHARE_MEDIA.WEIXIN, umAuthListener);
        //登录和授权都必须重写onActivityResult方法
//          QQ登录授权
//        mShareAPI.deleteOauth(this,SHARE_MEDIA.WEIXIN,umAuthListener);
//        if (UserConfig.getInstance().getOpenId() == null || "".equals(UserConfig.getInstance().getOpenId())) {
//            SHARE_MEDIA platform = SHARE_MEDIA.WEIXIN;
//            mShareAPI.doOauthVerify(this, platform, umAuthListener);
//        } else {
////        以下为分享按钮点击事件中调用的分享接口
//            UMImage image = new UMImage(TestActivity.this, "http://www.umeng.com/images/pic/social/integrated_3.png");
//            new ShareAction(this)
//                    .setPlatform(SHARE_MEDIA.WEIXIN)
//                    .setCallback(umShareListener)
//                    .withText("hello umeng video")
//                    .withTargetUrl("http://www.baidu.com")
//                    .withMedia(image)
//                    .share();
//        }
    }

    private void init() {
    }

    public void doLogin() {
        LoginInfo info = new LoginInfo(UserConfig.getInstance().getNet_ease(), UserConfig.getInstance().getNet_token()); // config...
        RequestCallback<LoginInfo> callback =
                new RequestCallback<LoginInfo>() {
                    @Override
                    public void onSuccess(LoginInfo param) {
                        UserConfig.getInstance().setIMLoginInfo(param);
                        doTest();
                    }

                    @Override
                    public void onFailed(int code) {
                        doTest();
                    }

                    @Override
                    public void onException(Throwable exception) {
                        doTest();
                    }
                    // 可以在此保存LoginInfo到本地，下次启动APP做自动登录用
                };
        NIMClient.getService(AuthService.class).login(info)
                .setCallback(callback);
    }

//    final Random random = new Random();
//    final int max = 3 * 60 * 1000;
//    final int min = 60 * 1000;
//
//    android.os.Handler handler = new android.os.Handler();
//
//    Runnable runnable = new Runnable() {
//        @Override
//        public void run() {
//            request(new BaseBodyParams(Constant.UrlParams.GETROBOTS), BaseResponse.class, listener, errorListener);
//        }
//    };
//
//    Response.Listener<BaseResponse> listener = new Response.Listener<BaseResponse>() {
//        @Override
//        public void onResponse(BaseResponse onlyStatusResponse) {
//            handler.postDelayed(runnable, random.nextInt(max) % (max - min + 1) + min);
//        }
//    };
//
//    Response.ErrorListener errorListener = new Response.ErrorListener() {
//        @Override
//        public void onErrorResponse(VolleyError volleyError) {
//
//        }
//    };

    private void doTest() {

        // 发牌接口
        request(new BaseBodyParams(Constant.UrlParams.GAME_DEAL), DealResponse.class, new Response.Listener<DealResponse>() {
            @Override
            public void onResponse(DealResponse baseResponse) {
                if (baseResponse.getStatusCode() == 6) {

                }

            }
        });
        //下注游戏id
        long niu_sid = 111l;
        //赌注数组，从左到右3副牌赌注
        int[] a = {};
        request(new BetParams(niu_sid, a), DealResponse.class, new Response.Listener<DealResponse>() {
            @Override
            public void onResponse(DealResponse baseResponse) {
                if (baseResponse.getStatusCode() == 6) {

                }

            }
        });


        //批量获取个人信息接口
//        request(new GetInfosParams(new String[]{"516262908694857"}), GetInfosResponse.class, this);

        request(new GetInfosParams(new String[]{"516262908694857"}), GetInfosResponse.class, this);
        //机器人接口
//        request(new BaseBodyParams(Constant.UrlParams.GETROBOTS), BaseResponse.class, listener, errorListener);

        //心跳接口
//        request(new BaseBodyParams(Constant.UrlParams.HEARTBEAT), BaseResponse.class, new Response.Listener<BaseResponse>() {
//            @Override
//            public void onResponse(BaseResponse onlyStatusResponse) {
//
//            }
//        }, new Response.ErrorListener() {
//            @Override
//            public void onErrorResponse(VolleyError volleyError) {
//
//            }
//        });
        //获取用户金币
//        BaseBodyParams b = new BaseBodyParams(UserConfig.getInstance().getSession_token());
//        b.setUrl(Constant.UrlParams.MY_COINS);
//        request( b, CoinsResponse.class, this);
        //账号比对接口
//        request(new ValiidParams(UserConfig.getInstance().getUserId()), OnlyStatusResponse.class, this);

        //注册接口
//        request(new RegParams(18652928466l,"8654","sdfsaf","sdfsdfs"), OnlyStatusResponse.class, this);

        //获取验证码接口
//        request(new GetcodeParams(UserConfig.getInstance().getUserId(), "regist"), OnlyStatusResponse.class, this);
        //对比验证码接口
//        request(new ValicodeParams(UserConfig.getInstance().getUserId(),"5564", "regist"), OnlyStatusResponse.class, this);
        //重置密码接口
//        request(new ResetParams(UserConfig.getInstance().getUserId(),"000000"), OnlyStatusResponse.class, this);

        //直播间周期更新观众人数接口
//        request(new AddnumParams(5), OnlyStatusResponse.class, this);
        //直播间验证接口
//        request(new ValidateParams(UserConfig.getInstance().getUserId(), "rtmp://v5886108b.live.126.net/live/41039de4f9e34015abb6eae34598560e"), ValidateResponse.class, this);

        //	用户设置接口
//        request(new MysettingParams(true, JPushInterface.getRegistrationID(getApplicationContext())), OnlyStatusResponse.class, this);

//        request(new ThirdPayParams("23423", "", 1, 2), ThirdPayResponse.class, this);
//	fans消费接口（送出虚拟币）
//        GiftParams giftParams = new GiftParams(24234234L, 345);
//        giftParams.setUrl(Constant.UrlParams.ADDGIFT);
//        request(giftParams, OnlyStatusResponse.class, this);

        //	fans增加分享次数接口
//        GiftParams giftParams = new GiftParams(24234234L);
//        giftParams.setUrl(Constant.UrlParams.ADDSHARE);
//        request(giftParams, OnlyStatusResponse.class, this);

        //	fans增加时长接口
//        GiftParams giftParams = new GiftParams(24234234L,1000L);
//        giftParams.setUrl(Constant.UrlParams.ADDDURATION);
//        request(giftParams, OnlyStatusResponse.class, this);

        //直播间关闭后用户界面接口
//        BaseBodyParams b = new BaseBodyParams(UserConfig.getInstance().getSession_token());
//        b.setUrl(Constant.UrlParams.CLOSEFAN);
//        request( b, HotLiveResponse.class, this);

        //直播间关闭后用户界面接口
//        BaseBodyParams b = new BaseBodyParams();
//        b.setUrl(Constant.VERSION + Constant.UrlParams.ANCHORVALIDATE);
//        request(b, OnlyStatusResponse.class, this);

        //直播间准备关闭接口
//        BaseBodyParams b = new BaseBodyParams(UserConfig.getInstance().getSession_token());
//        b.setUrl(Constant.UrlParams.PRECLOSE);
//        request( b, PrecloseResponse.class, this);

        //直播间关闭接口
//        BaseBodyParams b = new BaseBodyParams(UserConfig.getInstance().getSession_token());
//        b.setUrl(Constant.UrlParams.LIVECLOSE);
//        request( b, OnlyStatusResponse.class, this);

        //直播间礼物列表接口
//        BaseBodyParams b = new BaseBodyParams(UserConfig.getInstance().getSession_token());
//            b.setUrl(Constant.UrlParams.LIVEGIFTS);
//        request( b, LiveGiftResponse.class, this);

        //准备间返回热门子标签接口
//        request(new PrelabelsParams(3, 20), MoreFilterResponse.class, this);

        //直播间准备接口
//        BaseBodyParams b = new BaseBodyParams(UserConfig.getInstance().getSession_token());
//        b.setUrl(Constant.VERSION + Constant.UrlParams.LIVEPRE);
//        request(b, LivePreResponse.class, this);

        //用户关注主播/取关的接口
        //[a,b]=====>a:(1代表关注，0代表取关);b:(1代表直播间内关注，0代表其他)
//        int a[] = {0, 0};
//        request(new IsfocusParams(13912345678L, a), OnlyStatusResponse.class, this);

        //用户反馈接口
//        request(new FeedbackParams("good good study，day day up"), OnlyStatusResponse.class, this);

        //fans分享接口
//        int a[] = {3, 3};
//        GetPageParams gp = new GetPageParams(a);
//        gp.setUrl(Constant.UrlParams.FANSSHARE);
//        request(gp, GetFocusResponse.class, this);

        //fans时长接口
//        int a[] = {3, 3};
//        GetPageParams gp = new GetPageParams(a);
//        gp.setUrl(Constant.UrlParams.FANSDURATION);
//        request(gp, GetFocusResponse.class, this);

        //fans贡献接口
//        int a[] = {3, 3};
//        GetPageParams gp = new GetPageParams(a);
//        gp.setUrl(Constant.UrlParams.GIFT);
//        request(gp, GetFocusResponse.class, this);

        //用户获取关注主播的信息接口
//        int a[] = {3, 3};
//        GetPageParams gp = new GetPageParams(a);
//        gp.setUrl(Constant.UrlParams.PRIVATEFOCUS);
//        request(gp, GetFocusResponse.class, this);

        //用户获取关注自己的fans信息接口
//        int a[] = {3, 3};
//        GetPageParams gp = new GetPageParams(a);
//        gp.setUrl(Constant.UrlParams.PRIVATEFANS);
//        request(gp, GetFocusResponse.class, this);

        //用户获取自身等级接口
//        BaseBodyParams b = new BaseBodyParams(UserConfig.getInstance().getSession_token());
//        b.setUrl(Constant.UrlParams.PRIVATELEVEL);
//        request( b, Level.class, this);

        //个人信息更新接口
//        request(new PersonalInfo(UserConfig.getInstance().getSession_token(), UserConfig.getInstance().getUserId(), "lining...", "f",
//                "************", "http://bbs.feng.com/data/attachment/forum/201403/12/234210e0kgkykbscamqbsj.jpg",
//                "江苏省|南京市"), OnlyStatusResponse.class, this);
        //查询系统消息
//        NoticeParams noticeParams = new NoticeParams(0,0,1);
        //没有做参数强制检查，什么都不传，默认返回所有记录，指定id则查询详细
//        BaseBodyParams b = new BaseBodyParams(UserConfig.getInstance().getSession_token());
//        b.setUrl(Constant.UrlParams.NOTICE);
//        request(b, NoticeResponse.class, new Response.Listener<NoticeResponse>() {
//            @Override
//            public void onResponse(NoticeResponse hotLiveResponse) {
//                Log.e("NoticeResponse", hotLiveResponse.getHash().size() + "");
//            }
//        });
        //首页获取数据接口
//            request(new IndexParams(loginResponse.getSession_token()), IndexResponse.class, this);

        //热门直播接口
//            int a[] = {1, 3};
//            request(new HotLiveParams(a), HotLiveResponse.class, new Response.Listener<HotLiveResponse>() {
//                @Override
//                public void onResponse(HotLiveResponse hotLiveResponse) {
//                    hotLiveResponse.getHash();
//                }
//            });


        //更多
//            int a[] = {1, 3};
//            request(new MoreParams("美女", a), HotLiveResponse.class, new Response.Listener<HotLiveResponse>() {
//                @Override
//                public void onResponse(HotLiveResponse hotLiveResponse) {
//                    hotLiveResponse.getHash();
//                }
//            });
        //关注栏目
//            request(new FocusParams(loginResponse.getSession_token()), FocusResponse.class, this);

        //栏目修改接口
//            int a[] = {1, 3};
//            request(new FocusEditParams(loginResponse.getSession_token(), a), OnlyStatusResponse.class, this);
//热门约播
//        int a[] = {5, 5};
//        GetPageParams pageParams = new GetPageParams(a);
//        pageParams.setUrl(Constant.VERSION + Constant.UrlParams.HOTHOOKUP);
//        //热门约播取hot_hook_ups字段的值
//        request(pageParams, HookupsResponse.class, this);


        //进行中约播
//        BaseBodyParams b = new BaseBodyParams(UserConfig.getInstance().getSession_token());
//        b.setUrl(Constant.VERSION + Constant.UrlParams.DOING);
//        request(b, HookupsResponse.class, this);
//主播资料
//        request(new AnchorParams(3), AnchorInfoResponse.class, this);

        //进入过滤界面接口
//        BaseBodyParams b = new BaseBodyParams(UserConfig.getInstance().getSession_token());
//        b.setUrl(Constant.VERSION + Constant.UrlParams.GOFILTER);
//        request(b, GoFilterResponse.class, this);

        //过滤接口
//        int a[] = {5, 5};
//        HookupMoreParams pageParams = new HookupMoreParams(a, "v", 1);
//        pageParams.setUrl(Constant.VERSION + Constant.UrlParams.DOINGFILTER);
//        //返回结果取hook_up_infos字段的值
//        request(pageParams, HookupsResponse.class, this);
//
//        //发起约播
//        List<PlanDate> dates = new ArrayList<PlanDate>();
//        dates.add(new PlanDate("2016-11-15 13:20:55", "2016-11-15 18:20:55"));
//        dates.add(new PlanDate("2016-11-16 13:20:55", "2016-11-16 18:20:55"));
//        request(new CreateHookupParams(1000, 0, "test", 1, "3", 1, dates), OnlyStatusResponse.class, this);

        // 用户认购
//        request(new BoughtParams(3, 2), OnlyStatusResponse.class, this);

        //主播发起开播权限验证
//        BaseBodyParams b = new BaseBodyParams(UserConfig.getInstance().getSession_token());
//        b.setUrl(Constant.VERSION + Constant.UrlParams.CHECK_PERMISSIONS);
//        request(b, OnlyStatusResponse.class, this);

        //我的约播
//        BaseBodyParams b = new BaseBodyParams(UserConfig.getInstance().getSession_token());
//        b.setUrl(Constant.VERSION + Constant.UrlParams.MYHOOPUP);
//        request(b, MyhookupResponse.class, this);

        //我的订阅
//        int a[] = {3, 3};
//        GetPageParams gp = new GetPageParams(a);
//        gp.setUrl(Constant.VERSION +Constant.UrlParams.MYSUBSCRIPTION);
//        request(gp, MysubscriptionResponse.class, this);

        //正在播出
//        BaseBodyParams baseBodyParams = new BaseBodyParams();
//        baseBodyParams.setUrl(Constant.VERSION + Constant.UrlParams.LIVES);
//        request(baseBodyParams, IndexAllResponse.class, new Response.Listener<IndexAllResponse>() {
//            @Override
//            public void onResponse(IndexAllResponse indexAllResponse) {
//                indexAllResponse.getHash();
//            }
//        });
//        正在播出（新版本）
//        int a[] = {3, 3};
//        GetPageParams gp = new GetPageParams(a);
//        gp.setUrl(Constant.VERSION + Constant.UrlParams.LIVES);
//        request(gp, HotLiveResponse.class, new Response.Listener<HotLiveResponse>() {
//            @Override
//            public void onResponse(HotLiveResponse indexAllResponse) {
//                indexAllResponse.getHash();
//            }
//        });

        //约播更多
//        int a[] = {5, 5};
//        HookupMoreParams pageParams = new HookupMoreParams("v",a);
//        pageParams.setUrl(Constant.VERSION + Constant.UrlParams.DOINGMORE);
//        //热门约播取hot_hook_ups字段的值
//        request(pageParams, HookupMoreResponse.class, this);

        //正在播出更多
//        int a[] = {5, 5};
//        HookupMoreParams pageParams = new HookupMoreParams(1,a);
//        pageParams.setUrl(Constant.VERSION + Constant.UrlParams.LIVEFILTER);
//        request(pageParams, HotLiveResponse.class, this);

        //获取过滤总数
//        request(new IndexFilterCountParams(1), SumResponse.class, this);

//            进入搜索界面调用接口
//            request(new GosearchParams(), GosearchResponse.class, new Response.Listener<GosearchResponse>() {
//                @Override
//                public void onResponse(GosearchResponse gosearchResponse) {
//                    Log.e("gosearchResponse", gosearchResponse.getHash().size() + "");
//                }
//            });

//            进入更多过滤界面调用接口
//            request(new MorefilterParams(1), MoreFilterResponse.class, new Response.Listener<MoreFilterResponse>() {
//                @Override
//                public void onResponse(MoreFilterResponse moreFilterResponse) {
////                    Log.e("gosearchResponse", moreFilterResponse.getHash().size() + "");
//                }
//            });

//            进入个人信息接口
//            request(new PersonalParams(loginResponse.getSession_token()), PersonalResponse.class, this);

        //搜索接口
//            request(new SearchParams("美女"), HotLiveResponse.class, this);

        //更多界面，筛选接口
//            String location;
//            if (BaseApplication.getApp().getMlocation() != null) {
//                location = BaseApplication.getApp().getMlocation().getCity();
//            } else {
//                location = "南京市";
//            }
//
//            long[] a = {2};
//            int[] b = {1,11};
        //如果用户未在界面选择，三个参数都有默认值，location为当前城市，
        //sl_assort_id第一位为首页选中的一级类别，第二位为当前界面选中的二级类别，如未选择，则没有第二个元素
        //rows默认值为｛1，50｝
//            request(new IndexFilterParams(location, a, b), HotLiveResponse.class, this);

        //更多界面，筛选数目接口
//            String location;
//            if (BaseApplication.getApp().getMlocation() != null) {
//                location = BaseApplication.getApp().getMlocation().getCity();
//            } else {
//                location = "南京市";
//            }
//
//            request(new IndexFilterCountParams(location, a, b), SumResponse.class, this);

        //文件上传
        //("video_pic"代表切片；"avatar"代表头像)
//            String body = new FileReqPackage(new UploadFileParams("avatar", UserConfig.getInstance().getSession_token())).packetParam();
//            new Thread(new UploadFile(Constant.URL + Constant.UrlParams.UPLOAD, "/storage/emulated/0/DCIM/Camera/test.png", body, new UploadCallBack() {
//                @Override
//                public void response(UploadResponse response) {
//                    Log.e("response", response.getHash().getStatusCode() + "||" + response.getHash().getAvatar_url());
//                }
//            })).start();

        //直播间创建接口（开始直播）
//        Slassort slassort = new Slassort(2, "美女");
//        List<Label> labels = new ArrayList<Label>();
//        labels.add(new Label(1, "游戏"));
//        request(new CreateRoomParams("***测试房间 description***", "南京市", UserConfig.getInstance().getSession_token(), 2, slassort, labels),
//                CreateRooomResponse.class, this);
//进入直播间
//        request(new GointoRoomParams(17312236861l, 51),
//                GointoRoomResponse.class, this);


    }

    UMShareListener umShareListener = new UMShareListener() {
        @Override
        public void onResult(SHARE_MEDIA platform) {
            Toast.makeText(TestActivity.this, platform + " 分享成功啦", Toast.LENGTH_SHORT).show();
        }

        @Override
        public void onError(SHARE_MEDIA platform, Throwable t) {
            Toast.makeText(TestActivity.this, platform + " 分享失败啦", Toast.LENGTH_SHORT).show();
        }

        @Override
        public void onCancel(SHARE_MEDIA platform) {
//            Toast.makeText(TestActivity.this, platform + " 分享取消了", Toast.LENGTH_SHORT).show();
        }
    };

    private UMAuthListener umAuthListener = new UMAuthListener() {
        @Override
        public void onComplete(SHARE_MEDIA platform, int action, Map<String, String> data) {
            Toast.makeText(getApplicationContext(), "Authorize succeed", Toast.LENGTH_SHORT).show();
//            Toast.makeText(getApplicationContext(), data.toString(), Toast.LENGTH_SHORT).show();

            if (action == UMAuthListener.ACTION_AUTHORIZE) {
                //授权成功后，获取信息
                mShareAPI.getPlatformInfo(TestActivity.this, platform, umAuthListener);
                UserConfig.getInstance().setOpenId("openid");
                UserConfig.getInstance().setThirdTocken("access_token");
            }
        }

        @Override
        public void onError(SHARE_MEDIA platform, int action, Throwable t) {
            Toast.makeText(getApplicationContext(), "Authorize fail", Toast.LENGTH_SHORT).show();
        }

        @Override
        public void onCancel(SHARE_MEDIA platform, int action) {
            Toast.makeText(getApplicationContext(), "Authorize cancel", Toast.LENGTH_SHORT).show();
        }
    };


    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        mShareAPI.onActivityResult(requestCode, resultCode, data);
    }

    @Override
    public void onResponse(BaseResponse baseResponse) {
        if (baseResponse instanceof LoginResponse) {
            LoginResponse loginResponse = (LoginResponse) baseResponse;
            UserConfig.getInstance().setSession_token(loginResponse.getSession_token());
            UserConfig.getInstance().setNet_ease(loginResponse.getNet_ease());
            UserConfig.getInstance().setNet_token(loginResponse.getNet_token());
            UserConfig.getInstance().setUserId(13933333333L);
            doLogin();
        } else {

//         热门直播接口
//            List<LiveRoom> retList = BaseApplication.mGson.fromJson(baseResponse.getHash(),
//                    new TypeToken<List<LiveRoom>>() {
//                    }.getType());

            //关注栏目接口
//            FocusResponse response = (FocusResponse) baseResponse;

            //栏目修改接口
//            if (Constant.RESULTSUC == ((OnlyStatusResponse) baseResponse).getStatusCode()) {
//                Log.e("RESULTSUC", "success");
//            }

            //进入个人信息接口
//            PersonalResponse response = (PersonalResponse) baseResponse;
        }
    }

    @Override
    public void onClick(View view) {
    }

}
